package com.ebocy.hammer.core;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.mina.core.session.IoSession;

import redis.clients.jedis.Jedis;

import com.alibaba.fastjson.JSON;
import com.ebocy.hammer.core.constants.GameConstants;
import com.ebocy.hammer.core.domain.Block;
import com.ebocy.hammer.core.domain.GamePlayer;
import com.ebocy.hammer.core.domain.Hammer;
import com.ebocy.hammer.core.domain.Item;
import com.ebocy.hammer.core.domain.Rank;
import com.ebocy.hammer.core.domain.Role;
import com.ebocy.hammer.core.domain.Room;
import com.ebocy.hammer.core.domain.Shop;
import com.ebocy.hammer.core.util.JedisUtil;

public class GameCache {
	private static GameCache instance = new GameCache();

	public static GameCache getInstance() {
		return instance;
	}

	public static int incr;

	public static int team_member;
	// public static Map<Integer, Room> room4V4Map = new
	// ConcurrentHashMap<Integer, Room>();
	// public static Queue<Room> room4V4Queue = new
	// ConcurrentLinkedQueue<Room>();
	public static final String BATTLE_TEAM = "battle_team";
	public static final String QUEUE = "queue";
	public static final String INCR = "incr";
	public static final String TEAM_MEMBER = "team_member";

	public static Map<String, List<Block>> blockMap = null;
	public static Map<Integer, Hammer> hammerData = new HashMap<Integer, Hammer>();
	public static Map<Integer, Item> itemData = new HashMap<Integer, Item>();
	public static Map<Integer, Role> roleData = new HashMap<Integer, Role>();
	public static Map<Integer, Shop> shopData = new HashMap<Integer, Shop>();
	public static Map<Integer, Rank> rankData = new HashMap<Integer, Rank>();

	public static int getIncr() {
		Jedis jedis = JedisUtil.getJedis();
		incr = jedis.incr(INCR).intValue();
		JedisUtil.closeJedis(jedis);
		return incr;
	}

	public static int getTeamMember() {
		Jedis jedis = JedisUtil.getJedis();
		if (team_member == 0) {
			team_member = GameConstants.TEAM_MEMBERS_TOP_LIMIT;
			jedis.set(TEAM_MEMBER, String.valueOf(team_member));
		} else {
			team_member = Integer.valueOf(jedis.get(TEAM_MEMBER));
		}
		JedisUtil.closeJedis(jedis);
		return team_member;
	}

	public static int getRoomIncr(int roomId) {
		Jedis jedis = JedisUtil.getJedis();
		int roomincr = jedis.incr(String.valueOf(roomId)).intValue();
		JedisUtil.closeJedis(jedis);
		return roomincr;
	}

	public static void setRoom(Room room) {
		Jedis jedis = JedisUtil.getJedis();
		jedis.hset(BATTLE_TEAM, String.valueOf(room.roomId), JSON.toJSONString(room));
		JedisUtil.closeJedis(jedis);
	}

	public static void delRoom(int roomId) {
		Jedis jedis = JedisUtil.getJedis();
		jedis.hdel(BATTLE_TEAM, String.valueOf(roomId));
		jedis.del(String.valueOf(roomId));
		JedisUtil.closeJedis(jedis);
	}

	public static Room getRoom(int roomId) {
		Jedis jedis = JedisUtil.getJedis();
		String jsonString = jedis.hget(BATTLE_TEAM, String.valueOf(roomId));
		JedisUtil.closeJedis(jedis);
		return (Room) JSON.parseObject(jsonString, Room.class);
	}

	public static int getQueue() {
		Jedis jedis = JedisUtil.getJedis();
		int roomId = Integer.valueOf(jedis.lindex(QUEUE, 0));
		JedisUtil.closeJedis(jedis);
		return roomId;
	}

	public static void addQueue(int roomId) {
		Jedis jedis = JedisUtil.getJedis();
		jedis.lpush(QUEUE, String.valueOf(roomId));
		JedisUtil.closeJedis(jedis);
	}

	public static void delQueue() {
		Jedis jedis = JedisUtil.getJedis();
		jedis.rpop(QUEUE);
		JedisUtil.closeJedis(jedis);
	}

	public static long queueLen() {
		Jedis jedis = JedisUtil.getJedis();
		long length = jedis.llen(QUEUE);
		JedisUtil.closeJedis(jedis);
		return length;
	}

	public static Set<Room> getRoomInfo() {
		Jedis jedis = JedisUtil.getJedis();

		Map<String, String> queue = jedis.hgetAll(BATTLE_TEAM);
		JedisUtil.closeJedis(jedis);
		Set<String> queueSet = queue.keySet();
		Set<Room> roomSet = new HashSet<Room>();
		for (String temp : queueSet) {
			Room room = (Room) JSON.parse(queue.get(temp));
			roomSet.add(room);
		}
		return roomSet;
	}

	private Map<Long, GamePlayer> onlineGamePlayers = new HashMap<Long, GamePlayer>();
	private Map<Long, GamePlayer> GamePlayersCache = new HashMap<Long, GamePlayer>();

	public GamePlayer getGamePlayerBySession(IoSession session) {
		return onlineGamePlayers.get(session.getId());
	}

	public Map<Long, GamePlayer> getOnlineGamePlayers() {
		return onlineGamePlayers;
	}

	public void addCache(GamePlayer gamePlayer) {
		this.GamePlayersCache.put(gamePlayer.getGamePlayerId(), gamePlayer);
	}

	public GamePlayer getCache(Long gamePlayerId) {
		return this.GamePlayersCache.get(gamePlayerId);
	}

	public void delCache(Long gamePlayerId) {
		this.GamePlayersCache.remove(gamePlayerId);
	}

	public void addGamePlayer(GamePlayer gamePlayer) {
		this.onlineGamePlayers.put(gamePlayer.getSessionId(), gamePlayer);
	}

	public void removeGamePlayer(GamePlayer gamePlayer) {
		this.onlineGamePlayers.remove(gamePlayer.getSessionId());
	}
}
